package io.renren.modules.mysys.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.mysys.entity.MySysRoleEntity;
import io.renren.modules.mysys.service.MySysRoleService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author tanyajun
 * @email tan82692586@gmail.com
 * @date 2021-05-17 13:34:13
 */
@RestController
@RequestMapping("mysys/sysrole")
public class MySysRoleController {
    @Autowired
    private MySysRoleService sysRoleService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("mysys:sysrole:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = sysRoleService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("mysys:sysrole:info")
    public R info(@PathVariable("id") Integer id){
		MySysRoleEntity sysRole = sysRoleService.getById(id);

        return R.ok().put("sysRole", sysRole);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("mysys:sysrole:save")
    public R save(@RequestBody MySysRoleEntity sysRole){
		sysRoleService.save(sysRole);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("mysys:sysrole:update")
    public R update(@RequestBody MySysRoleEntity sysRole){
		sysRoleService.updateById(sysRole);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("mysys:sysrole:delete")
    public R delete(@RequestBody Integer[] ids){
		sysRoleService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @RequestMapping("/infobylevel/{level}")
    public R getInfoByLevel(@PathVariable("level") Integer level){
        QueryWrapper<MySysRoleEntity> wrapper = new QueryWrapper<>();
        if(level!=null){
            wrapper.eq("role_level",level);
            List<MySysRoleEntity> list = sysRoleService.list(wrapper);
            return R.ok().put("data", list);
        }
        return R.error().put("level",level);
    }
}
